package com.taiyi.renaissancemall.ui.mine.withdraw;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.taiyi.renaissancemall.R;
import com.taiyi.renaissancemall.Utils.RegexUtil;
import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.base.BaseActivity;
import com.taiyi.renaissancemall.base.MyApp;
import com.taiyi.renaissancemall.bean.MyScoresBean;
import com.taiyi.renaissancemall.bean.PersonInfoBean;
import com.taiyi.renaissancemall.bean.ReturnMoneyBean;
import com.taiyi.renaissancemall.bean.VerifiedBean;
import com.taiyi.renaissancemall.ui.mine.wallet.MyWalletActivity;
import com.taiyi.renaissancemall.ui.mine.wallet.withdraw.WithdrawActivity;
import com.taiyi.renaissancemall.ui.mine.wallet.withdraw.WithdrawPresent;
import com.taiyi.renaissancemall.ui.mine.wallet.withdraw.WithdrawView;
import com.taiyi.renaissancemall.ui.mine.withdraw.bindaccount.BindAccountActivity;
import com.taiyi.renaissancemall.wxapi.WXEntryActivity;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WithdrawSettingActivity extends BaseActivity<WithdrawView, WithdrawPresent> implements WithdrawView {

    @BindView(R.id.backIv)
    ImageView backIv;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.rightIv)
    ImageView rightIv;
    @BindView(R.id.aliPay)
    TextView aliPay;
    @BindView(R.id.aLipPayIV)
    ImageView aLipPayIV;
    @BindView(R.id.unBoundAliPay)
    TextView unBoundAliPay;
    @BindView(R.id.bindAliPayTv)
    TextView bindAliPayTv;
    @BindView(R.id.weChat)
    TextView weChat;
    @BindView(R.id.weChatIV)
    ImageView weChatIV;
    @BindView(R.id.unBoundWeChat)
    TextView unBoundWeChat;
    @BindView(R.id.bindWeChatTv)
    TextView bindWeChatTv;
    @BindView(R.id.top)
    ConstraintLayout top;
    private int whereCode = -1;
    private String phone;
    private String tel;
    private String openId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_withdraw_setting;
    }

    @Override
    public WithdrawPresent initPresenter() {
        return new WithdrawPresent();
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        titleTv.setText(getString(R.string.withdrawalSettings));
        tel = getIntent().getStringExtra("tel");
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(top).init();
    }

    @Override
    protected void initData() {
        whereCode = getIntent().getIntExtra("whereCode", -1);
        presenter.getWithdrawDetail();
    }

    @Override
    public void initEvent() {

    }

    @OnClick({R.id.backIv, R.id.bindAliPayTv, R.id.bindWeChatTv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.backIv:
                finish();
                break;
            case R.id.bindAliPayTv:
                Intent intent_ali = new Intent(WithdrawSettingActivity.this, BindAccountActivity.class);
                intent_ali.putExtra("txtype", "2");
                if (phone != null && !TextUtils.isEmpty(phone)) {
                    intent_ali.putExtra("phone", phone);
                } else {
                    intent_ali.putExtra("phone", tel);
                }
                startActivity(intent_ali);
                finish();
                break;
            case R.id.bindWeChatTv:
                MyApp.getOpenId = 2;
                IWXAPI iwxapi = MyApp.getWxApi();
                if (!iwxapi.isWXAppInstalled()) {
                    ToastUtils.showShortToast("您的设备未安装微信客户端");
                } else {
                    final SendAuth.Req req = new SendAuth.Req();
                    req.scope = "snsapi_userinfo";
                    req.state = "wechat_sdk_demo_test";
                    iwxapi.sendReq(req);
                }
                break;
        }
    }

    @Override
    public void getWithdrawSuccess(ReturnMoneyBean returnMoneyBean) {
        if (returnMoneyBean.user != null) {
            phone = returnMoneyBean.user.tel;
        } else {
            phone = tel;
        }
        if (returnMoneyBean.bindinglist != null) {
            for (ReturnMoneyBean.BindinglistBean b : returnMoneyBean.bindinglist) {
                if ("1".equals(b.type)) {
                    if (!TextUtils.isEmpty(b.realname)) {
                        unBoundWeChat.setText(b.realname);
                    } else {
                        unBoundWeChat.setText(b.payno);
                    }
                    bindWeChatTv.setText(R.string.changeBind);
                } else if ("2".equals(b.type)) {
                    if (RegexUtil.isMobileNO(b.payno)) {
                        unBoundAliPay.setText(RegexUtil.phoneNoHide(b.payno));
                    } else {
                        unBoundAliPay.setText(b.payno);
                    }
                    bindAliPayTv.setText(R.string.changeBind);
                } else {
                    unBoundAliPay.setText(getString(R.string.account_unBound));
                    unBoundWeChat.setText(getString(R.string.account_unBound));
                    bindAliPayTv.setText(R.string.toBind);
                    bindWeChatTv.setText(R.string.toBind);
                }
            }
        } else {
            unBoundAliPay.setText(getString(R.string.account_unBound));
            unBoundWeChat.setText(getString(R.string.account_unBound));
        }
    }

    @Override
    public void getWithdrawFailure(String msg) {
        ToastUtils.showShortToast(msg);
    }

    @Override
    public void toWithdrawSuccess(String msg_cn) {
        ToastUtils.showShortToast(msg_cn);
    }

    @Override
    public void toWithdrawFailure(String msg) {
        ToastUtils.showShortToast(msg);
    }

    @Override
    public void getNickName(String msg_cn) {
        hideProgress();
//        String getOpen_id = withdrawEvent.getOpen_id();
        Intent intent_wx = new Intent(WithdrawSettingActivity.this, BindAccountActivity.class);
        intent_wx.putExtra("txtype", "1");
        intent_wx.putExtra("getOpen_id", openId);
        intent_wx.putExtra("getNick_name", msg_cn);
        if (phone != null && !TextUtils.isEmpty(phone)) {
            intent_wx.putExtra("phone", phone);
        } else {
            intent_wx.putExtra("phone", tel);
        }
        startActivity(intent_wx);
        finish();
    }

    @Subscribe
    public void getOpenId(WithdrawEvent withdrawEvent) {
        if (withdrawEvent.getOpen_id() != null && !"".equals(withdrawEvent.getOpen_id())) {
            openId = withdrawEvent.getOpen_id();
            presenter.getUserInfo(withdrawEvent.getAccessToken(), withdrawEvent.getOpen_id());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        MyApp.getOpenId = 1;
    }
}
